package com.fhj.movie_main;

import java.util.LinkedList;
import java.util.List;

import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.fhj.movie.baidumap.LocationServices;
import com.fhj.movie.baidumap.LocationServices.ServiceCallBack;
import com.fhj.movie.fragment.FhFilmFragment;
import com.fhj.movie.fragment.HyyHuoDongFragment;
import com.fhj.movie.fragment.HyyMineFrgment;
import com.fhj.movie.fragment.JyjMovieFragment;
import com.fhj.movie.interfaces.MyApplication;
import com.fhj.movie_fhj_01.R;
import com.fhj.utils.ConnectivityUtils;
import com.umeng.analytics.MobclickAgent;

public class MainActivity extends FragmentActivity implements OnCheckedChangeListener {

	
	private RadioGroup tabbar;//底部标题栏
	private List<Fragment> fragments;//内容存储区
	
	private Intent intent;//启动服务的意图
	
	//二次点击退出程序，handler处理
	private static boolean isExit=false;//二次点击退出标识符
	private static Handler  handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			isExit= false;
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		
		tabbar=(RadioGroup)this.findViewById(R.id.tab_bar);
		tabbar.setOnCheckedChangeListener(this);
		
		fragments=new LinkedList<Fragment>();
		//在这里添加fragment
		fragments.add(new FhFilmFragment());
		fragments.add(new JyjMovieFragment());
		fragments.add(new HyyHuoDongFragment());
		fragments.add(new HyyMineFrgment());
		//====================
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true){
					while(ConnectivityUtils.isConnectivity(MainActivity.this)==0){
						Intent intent = new Intent();
						
						sendBroadcast(intent, null);
					}
				}
			}
		}).start();
		
		
		//默认加载第一个界面
		RadioButton button=(RadioButton) tabbar.getChildAt(0);
		button.setChecked(true);
		showFragment(0);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO 主界面底层菜单栏点击事件实现
		int childCount=group.getChildCount();
		int childindex=0;
		RadioButton btn=null;
		for(int i=0;i<childCount;i++){
			btn=(RadioButton)group.getChildAt(i);
			if (btn.isChecked()) {
				childindex=i;
				break;
			}
		}
		showFragment(childindex);
	}

	private void showFragment(int childindex) {
		// TODO 根据id切换不同的界面
		Fragment fragment=fragments.get(childindex);
		FragmentManager manager=getSupportFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.replace(R.id.main_fragmentchange, fragment);
		transaction.commit();
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO 实现两次点击退出界面
		if (keyCode==KeyEvent.KEYCODE_BACK) {
			exit();
			return  true;
		}
		return super.onKeyDown(keyCode, event);
	}
	private void exit() {
		// TODO 两次点击退出方法实现
		if (!isExit) {
			isExit=true;
			Toast.makeText(this, "再次点击退出应用", Toast.LENGTH_SHORT).show();
			handler.sendEmptyMessageDelayed(0, 2000);
		}else{
			MainActivity.this.finish();
		}
	}
	
	@Override
	protected void onResume() {
		// TODO 自动生成的方法存根
		super.onResume();
		MobclickAgent.onPageStart("Main");
		MobclickAgent.onResume(this);
		
		
		intent = new Intent(MainActivity.this, LocationServices.class);
//		startService(intent);
		bindService(intent, connection, BIND_AUTO_CREATE);
	}
	
	private ServiceConnection connection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO 自动生成的方法存根
			
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO 自动生成的方法存根
			LocationServices.LocationBinder binder = (LocationServices.LocationBinder) service;
			LocationServices services = binder.getServices();
			services.getLocation();
			services.setServiceCallBack(new ServiceCallBack() {
				
				@Override
				public void getLocationInfo(double latitude, double longitude,
						String address) {
					Toast.makeText(MainActivity.this, ""+latitude+","+longitude+","+address, Toast.LENGTH_SHORT).show();
					MyApplication application = (MyApplication) getApplication();
					application.setLatitude(latitude);
					application.setLongitude(longitude);
					application.setAddress(address);
				
				}
			});
		}
	};
	
	
	
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("Main");
		MobclickAgent.onPause(this);
	}
	@Override
	protected void onDestroy() {
//		stopService(intent);
		unbindService(connection);
		super.onDestroy();
	}
	
}
